
<template>
  <div class="app-container">
    <el-form :inline="true" :rules="rules" :model="searchParam" class="demo-form-inline">
      <el-form-item label="项目空间">
        <el-select v-model="searchParam.workspace_id" placeholder="请选择">
          <el-option
            v-for="item in workspaces"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上线时间">
        <el-date-picker
          v-model="searchParam.deploy_date"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchParam.status" multiple placeholder="请选择">
          <el-option
            v-for="item in statuslist"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchData()">查询</el-button>
        <el-button :loading="downloadLoading" style="margin:0 0 20px 20px;" type="primary" icon="el-icon-document" @click="handleDownload">
          查看服务列表
        </el-button>

      </el-form-item>
    </el-form>
    <el-tag type="success">命中条目: {{ tableData.length }}</el-tag>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="path"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="id" label="ID" />
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="owner" label="所有者" />
      <el-table-column prop="develpoer" label="开发者" />
      <el-table-column prop="status" label="状态" />
      <el-table-column prop="services" label="服务" />
    </el-table>
    <el-dialog
      title="服务列表"
      :visible.sync="dialogVisible"
    >

      <el-row>
        <el-col :span="16">
          <el-input
            ref="services"
            v-model="services"
            type="textarea"
            :rows="20"
            size="medium"
          />
        </el-col>
        <el-col :span="2" />
        <el-col :span="6">

          <el-tag type="success">命中条目: {{ services.split('\n').length }}</el-tag>
          <el-button @click="handleCopy()">复制</el-button>
          <el-divider />
          <el-tag type="success">命中条目: {{ blacklist.split('\n').length }}</el-tag>
          <el-input
            ref="blacklist"
            v-model="blacklist"
            type="textarea"
            :rows="10"
          />
        </el-col>
      </el-row>

    </el-dialog>
  </div>
</template>

<script>
import { getWorkspaces, getStories, getServices } from '@/api/eff/tapd'
import moment from 'moment'
import Vue from 'vue'
import VueClipboard from 'vue-clipboard2'

Vue.use(VueClipboard)

export default {
  data() {
    return {
      workspaces: [],
      tableData: [],
      tablePageSize: 10,
      tablePage: 1,
      tableTotal: 0,
      listLoading: true,
      downloadLoading: false,
      dialogVisible: false,
      services: '',
      blacklist: '',
      searchParam: { 'deploy_date': '', workspace_id: '' },
      formLabelWidth: '120px',
      statuslist: [
        { 'label': '待开发', 'value': 'audited' },
        { 'label': '待测试', 'value': 'status_9' },
        { 'label': '测试中', 'value': 'product_experience' },
        { 'label': '已上线', 'value': 'resolved' },
        { 'label': '开发中', 'value': 'developing' },
        { 'label': '待评审', 'value': 'auditing' }
      ],
      rules: {
        workspace_id: [{ required: true }],
        deploy_date: [{ required: true }]
      }
    }
  },
  created() {
    this.initSearch()
  },
  methods: {
    initSearch() {
      getWorkspaces().then(response => {
        this.workspaces = response.data
        this.searchParam.workspace_id = response.data[0].id
      })
      this.initDate()
    },
    initDate() {
      const day = new Date()
      const weekday = day.getDay()
      if (weekday === 1 || weekday === 2) {
        day.setDate(day.getDate() - weekday + 2)
      } else {
        day.setDate(day.getDate() - weekday + 4)
      }
      this.searchParam.deploy_date = moment(day).format('YYYY-MM-DD')
    },
    fetchData() {
      this.listLoading = true

      // const params = Object.assign(this.searchParam, { 'start': start, 'end': end })
      getStories(this.searchParam).then(response => {
        this.tableData = response.data
        this.listLoading = false
      })
    },
    checkSearchParam() {
      if (typeof (this.searchParam.workspace_id) === 'undefined' || typeof (this.searchParam.deploy_date) === 'undefined') {
        return false
      }
      return true
    },
    handleDownload() {
      getServices(this.searchParam).then(response => {
        const data = response.data
        this.services = data.pipeline.join('\n')
        this.blacklist = data.blacklist.join('\n')
        this.dialogVisible = true
      })
    },

    handleCopy() {
      this.$copyText(this.services).then(e => {
        this.$message({
          type: 'success',
          message: '复制成功'
        })
      }, function(e) {
        this.$message({
          type: 'error',
          message: '复制失败'
        })
      }
      )
    }
  }
}
</script>

<style>

</style>
